/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.srv.client.Globals;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;

public class Card implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8378055037219056742L;

	private static int KEY = 0;

	private String key;
	
	private int floating = 0;

	private List<Control> controls = new ArrayList<Control>();

	public Card() {
		this.setKey(""+(KEY++));
	}

	public Card(int floating, List<Control> controls) {
		this.floating = floating;
		this.setKey(""+(KEY++));
		this.setControls(controls);
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public List<Control> getControls() {
		return controls;
	}

	public void setControls(List<Control> controls) {
		this.controls = controls;
	}

	public Card copy() {
		final List<Control> controls = getControls();
		final List<Control> newList = new ArrayList<Control>(controls.size());
		for (Control control : controls)
			newList.add(control.copy());
		return new Card(floating, newList);	
	}

	public void addControl(int insertIndex, Control control) {
		final List<Control> controls = getControls();
		controls.add(insertIndex, control);
		setControls(controls);
	}

	public String getHtml(String name) {
		final StringBuilder sb = new StringBuilder();
		final List<Control> controls = getControls();
		int max = Math.min(controls.size(), Globals.maxLinesInIcon);

		for (int i=0; i<max; ++i) {
			final Control control = controls.get(i);
			sb.append(control.getHtml(name+"_"+i));
		}

		return sb.toString();		
	}		

	public String getToolIcon() {
		return "<div style=\"width:100%; border-style:solid; border-color:lightgrey; " +
				"border-width:1px;\">" + 
				getHtml("card_"+new Date().getTime()) + "</div>";
	}

	public String getDragIcon() {
		return "<table style=\"border-style:solid; border-color:lightgrey; " +
				"border-width:1px;\"><tr><td>" + 
				getHtml("card_"+new Date().getTime()) + "</td></tr></table>";
	}

	public String getXML(String name) {
		final Document doc = XMLParser.createDocument();
		com.google.gwt.xml.client.Element element = doc.createElement("card");
		element.setAttribute("name", name);
		element.setAttribute("floating", ""+floating);
		final List<Control> controls = getControls();
		for (Control control : controls) {
			com.google.gwt.xml.client.Element controlElement = doc.createElement("control");
			controlElement.setAttribute("type", control.getTypeString());
			controlElement.setAttribute("value", control.getValue());
			controlElement.appendChild(doc.createTextNode(control.getText()));
			element.appendChild(controlElement);
		}
		return element.toString().replaceAll("><", ">\n<");
	}

	public int getFloating() {
		return floating;
	}

	public void setFloating(int floating) {
		this.floating = floating;
	}
	
}
